using System;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Windows.Forms;

namespace TTDotNet.Engine.ThreeDEngine
{
    public class AJVertexGroup : AJObject
    {
        private CustomVertex.PositionTextured[] verts;
        private Texture myTexture;

        private VertexBuffer vb;

        public AJVertexGroup(CustomVertex.PositionTextured[] vertex, Device myDevice)
        {
            verts = vertex;
            //ReloadResources(myDevice); // This way we don't have to type the buffer code twice
            //radius = (float)Math.Sqrt(2);
        }

        /*
        private void ComputeRadius()
        {
            using (vb)
            {
                GraphicsStream gStream = vb.Lock(0, 0, LockFlags.None);
                Vector3 tempCenter;

                radius = Geometry.ComputeBoundingSphere(gStream, verts.Length, VertexFormats.Position, out tempCenter);
                                
                vb.Unlock();
            }
            System.Windows.Forms.MessageBox.Show(radius.ToString());
        }
         */

        public override void Render(AJCamera camera)
        {
            if (camera.IsInViewFrustum(this) == false)
                return;

            Game.device.VertexFormat = CustomVertex.PositionTextured.Format;
            Game.device.SetTexture(0, myTexture);
            Game.device.SetStreamSource(0, vb, 0);

            Game.device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
        }
         

        public override void setTexture(Device device, string path)
        {
            myTexture = TextureLoader.FromFile(device, path);
        }

        public override bool Pick(Vector3 rayStart, Vector3 rayDirection, AJCamera camera, out IntersectInformation intInf)
        {
            // Convert ray to model space
            Matrix World =
                Matrix.Scaling(myScaling) *
                Matrix.RotationYawPitchRoll(myRotation.Y, myRotation.X, myRotation.Z) *
                Matrix.Translation(myPosition);

            Matrix inverseWorld = Matrix.Invert(World);

            Vector3 localStart = Vector3.TransformCoordinate(rayStart, inverseWorld);
            Vector3 localDirection = Vector3.TransformNormal(rayDirection, inverseWorld);
            localDirection.Normalize();

            // Check for mesh intersection
            //return myMesh.Intersect(localStart, localDirection, out intInf);

            bool check = false;
            IntersectInformation result = new IntersectInformation();

            for (int i = 0; i < verts.Length - 2; i++)
            {
                check = 
                Geometry.IntersectTri(  new Vector3(verts[i].X, verts[i].Y, verts[i].Z), 
                                        new Vector3(verts[i+1].X, verts[i+1].Y, verts[i+1].Z),
                                        new Vector3(verts[i+2].X, verts[i+2].Y, verts[i+2].Z), 
                                        localStart, localDirection, out result);
                if (check) break;

            }
            intInf = result;
            return check;

        }

    }

}
